<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/20
 * Time: 10:16
 */

class Forget extends M_Controller
{
    public function index(){
        $auth = $this->input->get('auth');
        if ($auth) {
            if ($auth != md5(SYS_KEY)) {
                // 授权认证码不正确
                echo $this->callback_json(array(
                    'msg' => '授权认证码不正确',
                    'code' => 0
                ));exit;
            }
            define('SELECT_API_AUTH', 1);
        }

        $step = max(1, (int)$this->input->get('step'));
        $error = '';


        if(IS_POST){
            $mobile = $this->input->get_post('phone');
            $code = $this->input->get_post('code');
            $pass = $this->input->get_post('pass');
            if(empty($mobile)){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }
            if(empty($code)){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }
            if(empty($pass)){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }

            //验证短信是否正确
            $where = array();
            $where['phone'] = $mobile;
            $where['code'] = $code;
            $where['is_last'] = 1;
            //检验验证码是否有效
            $sms = $this->db->where($where)->get('sms_code')->row_array();
            if(!$sms){
                $error = array('status' => 0, 'msg' => fc_lang('验证码错误'));
            }
            $error && exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));

            $data = $this->db
                ->select('id,name,code,pass')
                ->where("mobile", $mobile)
                ->limit(1)
                ->get('user')
                ->row_array();
            if($data){
                $user = $this->db
                    ->select('id,name,code')
                    ->where("code", $code)
                    ->limit(1)
                    ->get('user')
                    ->row_array();
                $pas = $pass;
                $password = md5($pass);
                $newdata = array('pass'=>$password,"code"=>$code,"pas"=>$pas);
                // 修改密码
                $this->db->where('id', $data['id'])->update('user',$newdata);
                $this->get_cache('USER', 'setting', 'ucenter');
                //classin 修改
                $datas = array("SID"=>CLASSIN_SID,"safeKey"=>md5(CLASSIN_SAFEKEY.time()),"timeStamp"=>time(),"telephone"=>$mobile,"oldMd5pass"=>$data['pass'],"md5pass"=>$password);
                send_posts(CLASSIN_MODIFY_PWD,$datas);
                //classin结束
//                    $this->hooks->call_hook('user_edit_password', array('member' => $data, 'password' => $password1)); 
                $this->user_msg(fc_lang('找回密码成功'), dr_user_url('login/index'), 3,$_GET['debug'] ? 99999 : 3);
                exit('error');
            }else{
                $error = array('status' => 0, 'msg' => fc_lang('该号码尚未注册'));
                $error && exit($this->callback_json(array(
                    'msg' => $error['msg'],
                    'code' => 0
                )));
            }
        }
        $this->template->assign(array(
            'error' => $error,
            'action' => 'find',
            'mobile' => $this->get_cache('user', 'setting','ismobile'),
            'meta_title' => fc_lang('找回密码通知'),
            'result_error' => $error,
        ));
        $this->template->display("../gw/forget.html");
    }

    public function poup_forget(){
        $auth = $this->input->get('auth');
        if ($auth) {
            if ($auth != md5(SYS_KEY)) {
                // 授权认证码不正确
                echo $this->callback_json(array(
                    'msg' => '授权认证码不正确',
                    'code' => 0
                ));exit;
            }
            define('SELECT_API_AUTH', 1);
        }

        $step = max(1, (int)$this->input->get('step'));
        $error = '';


        if(IS_POST){
            $mobile = $this->input->get_post('phone');
            $code = $this->input->get_post('code');
            $pass = $this->input->get_post('pass');
            if(empty($mobile)){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }
            if(empty($code)){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }
            if(empty($pass)){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }

            //验证短信是否正确
            $where = array();
            $where['phone'] = $mobile;
            $where['code'] = $code;
            $where['is_last'] = 1;
            //检验验证码是否有效
            $sms = $this->db->where($where)->get('sms_code')->row_array();
            if(!$sms){
                $error = array('status' => 0, 'msg' => fc_lang('验证码错误'));
            }
            $error && exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));

            $data = $this->db
                ->select('id,name,code,pass')
                ->where("mobile", $mobile)
                ->limit(1)
                ->get('user')
                ->row_array();
            if($data){
                $user = $this->db
                    ->select('id,name,code')
                    ->where("code", $code)
                    ->limit(1)
                    ->get('user')
                    ->row_array();
                $pas = $pass;
                $password = md5($pass);
                $newdata = array('pass'=>$password,"code"=>$code,"pas"=>$pas);
                // 修改密码
                $this->db->where('id', $data['id'])->update('user',$newdata);
                $this->get_cache('USER', 'setting', 'ucenter');
                //classin 修改
                $datas = array("SID"=>CLASSIN_SID,"safeKey"=>md5(CLASSIN_SAFEKEY.time()),"timeStamp"=>time(),"telephone"=>$mobile,"oldMd5pass"=>$data['pass'],"md5pass"=>$password);
                send_posts(CLASSIN_MODIFY_PWD,$datas);
                //classin结束
//                    $this->hooks->call_hook('user_edit_password', array('member' => $data, 'password' => $password1));
                $this->user_msg(fc_lang('找回密码成功'), dr_user_url('login/index'), 3,$_GET['debug'] ? 99999 : 3);
                exit('error');
            }else{
                $error = array('status' => 0, 'msg' => fc_lang('该号码尚未注册'));
                $error && exit($this->callback_json(array(
                    'msg' => $error['msg'],
                    'code' => 0
                )));
            }
        }
        $this->template->assign(array(
            'error' => $error,
            'action' => 'find',
            'mobile' => $this->get_cache('user', 'setting','ismobile'),
            'meta_title' => fc_lang('找回密码通知'),
            'result_error' => $error,
        ));
        $this->template->display("../gw/poup_forget.html");
    }
}